"""
一个发送短信的辅助类

"""

from info.libs.CCPRestSDK import REST


class CF(object):
    AccountSid = '8aaf0708648862310164a0777be10e75'
    AccountToken = 'ea55f8b706a949dcbaf8cd39f1e5df90'
    AppId = '8aaf0708648862310164a0777c350e7b'
    ServerIP = 'app.cloopen.com'
    ServerPort = '8883'
    SoftVersion = '2013-12-26'
    BodyType = 'json'


class MSGHelp(REST):
    __instance = None
    __isfirst = True
    __cf = CF()

    def __new__(cls, *args, **kwargs):
        if not cls.__instance:
            # 调用父类的new方法
            # def __new__(cls)
            cls.__instance = REST.__new__(cls)
            return cls.__instance
        else:
            return cls.__instance

    def __init__(self):

        if MSGHelp.__isfirst:
            super().__init__(MSGHelp.__cf.ServerIP, MSGHelp.__cf.ServerPort, MSGHelp.__cf.SoftVersion)
            MSGHelp.__isfirst = False

        # 调用设置账户和设置APPID方法
        self.setAccount()
        self.setAppId()

    # 设置账户
    def setAccount(self):
        super().setAccount(MSGHelp.__cf.AccountSid, MSGHelp.__cf.AccountToken)

    # 设置APPID
    def setAppId(self):
        super().setAppId(MSGHelp.__cf.AppId)

    # 设置短信发送
    def sendTemplateSMS(self, to, datas, tempId):
        result = super().sendTemplateSMS(to, datas, tempId)
        if result.get("statusCode") == "000000":
            # 返回0 表示发送短信成功
            return 0
        else:
            # 返回-1 表示发送失败
            return -1


hp = MSGHelp()

if __name__ == '__main__':
    # 测试

    hp.sendTemplateSMS('17070855717', ['9999', 5], 1)
